package org.zcl.dao;

import org.zcl.entity.User;
import org.zcl.io.Resource;
import org.zcl.sqlSession.SqlSession;
import org.zcl.sqlSession.SqlSessionFactory;
import org.zcl.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * .
 *
 * @Author Chenglin Zhu
 * @Date 2022/3/13 22:35
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User findOne(User user) throws Exception {
        InputStream resourceAsStream = Resource.getResourceAsStream("sqlMapperConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        return userDao.findOne(user);
    }

    @Override
    public List<User> findAll() throws Exception {
        InputStream resourceAsStream = Resource.getResourceAsStream("sqlMapperConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        return userDao.findAll();
    }
}
